package com.lgl.scs.util.service;

import com.lgl.scsapi.base.dao.DaoSupport;
import com.lgl.scsapi.system.model.PageData;
import com.lgl.scsapi.util.utils.Constants;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;

/**
 * 工具service
 */

@Service("utilService")
public class UtilService {
	@Resource
	private RedisTemplate redisTemplate;
	@Resource(name = "daoSupport")
	private DaoSupport dao;
	
	/**
	 * 用户交易记录日志信息列表
	 * @param pageData 查询参数
	 * @return 返回List<PageData>
	 */
	public List<PageData> userDealslistPage(PageData pageData) {
		return (List<PageData>) dao.findForList("UtilMapper.userDealslistPage", pageData);
	}

	/**
	 * 插入数据
	 * @param insertSql 插入的SQL
	 * @return 插入的行数
	 */
	public int insertBySql(String insertSql){
		return dao.save("UtilMapper.insertObject", insertSql);
	}
	/**
	 * 修改数据
	 * @param updateSql 更新的SQL
	 * @return 更新的行数
	 */
	public int updateDataBySql(String updateSql){
		return dao.save("UtilMapper.updateDataBySql", updateSql);
	}

    /**
     * 备份用户删除的订单
	 * @param deleteOrderPd 要删除的参数
	 * @return 保存成功的行数
	 */
	public int backupDeleteOrder(PageData deleteOrderPd){
		if (deleteOrderPd == null || deleteOrderPd.isEmpty()){
			return 0;
		}
		return dao.save("UtilMapper.backupDeleteOrder", deleteOrderPd);
	}

	/**
	 * 异步设置用户登录状态
	 * @param userId 用户ID
	 */
	@Async
	public void asyncSetLoginStatus(String userId){
		long id = Long.valueOf(userId);
		Calendar calendar = Calendar.getInstance();
		int day = calendar.get(Calendar.DAY_OF_MONTH), month = calendar.get(Calendar.MONTH) + 1;
		redisTemplate.opsForValue().setBit(Constants.USER_LOGIN_SET_PRE + month + day,id,true);
	}
}